package com.fanhao.dllo.soundfm.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.fanhao.dllo.soundfm.R;
import com.fanhao.dllo.soundfm.activity.PlayActivity;
import com.fanhao.dllo.soundfm.entity.PlayActivityCommentsResult;
import com.fanhao.dllo.soundfm.fragment.BaseFragment;

import java.util.List;

/**
 * Created by dllo on 16/1/18.
 */
public class PlayActivityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private PlayActivityCommentsResult.CommentInfoEntity commentInfoEntity;
    private LayoutInflater inflater;

    public PlayActivityAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    // 获得CommentEntity的方法
    public void addCommentData(PlayActivityCommentsResult.CommentInfoEntity commentInfoEntity) {
        this.commentInfoEntity = commentInfoEntity;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            View view = inflater.inflate(R.layout.activity_play_adapter_item_head, parent, false);
            return new AdapterHeaderViewHolder(view);
        }
        if (viewType == 1) {
            View view = inflater.inflate(R.layout.activity_paly_adapter_item, parent, false);
            return new AdapterView(view);
        }
        if (viewType == 2) {
            View view = inflater.inflate(R.layout.activity_play_adapter_item_foot, parent, false);
            return new AdapterFootViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (commentInfoEntity != null) {
            List<PlayActivityCommentsResult.CommentInfoEntity.ListEntity> list = commentInfoEntity.getList();
            if (holder instanceof AdapterHeaderViewHolder) {
                AdapterHeaderViewHolder headerHolder = (AdapterHeaderViewHolder) holder;
                headerHolder.commentNumTv.setText("听众点评(" + commentInfoEntity.getTotalCount() + ")");
            }
            if (list != null && list.size() > 0) {
                if (holder instanceof AdapterView) {
                    AdapterView adaholder = (AdapterView) holder;
                    adaholder.nameTv.setText(list.get(position - 1).getNickname());
                    adaholder.commentTv.setText(list.get(position - 1).getContent());
                    adaholder.likeNumTv.setText(list.get(position - 1).getLikes() + " ");
                    adaholder.userIv.setImageUrl(list.get(position - 1).getSmallHeader(), BaseFragment.getImageLoader());
                }
            }
            if (holder instanceof AdapterFootViewHolder) {

            }
        }
    }

    @Override
    public int getItemCount() {
        return commentInfoEntity != null && commentInfoEntity.getList().size() + 2 > 0 ?
                commentInfoEntity.getList().size() + 2 : 0;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) return 0;
        if (position > 0 && position < commentInfoEntity.getList().size() + 1) return 1;
        if (position == commentInfoEntity.getList().size() + 1) return 2;

        return position;
    }

    // 多少条评论
    class AdapterHeaderViewHolder extends RecyclerView.ViewHolder {
        private TextView commentNumTv;

        public AdapterHeaderViewHolder(View itemView) {
            super(itemView);
            commentNumTv = (TextView) itemView.findViewById(R.id.tvCommentNum_playActivityAdapter);
        }
    }


    class AdapterView extends RecyclerView.ViewHolder {
        private TextView nameTv, commentTv, likeNumTv;
        private NetworkImageView userIv;

        public AdapterView(View itemView) {
            super(itemView);
            nameTv = (TextView) itemView.findViewById(R.id.tvName_playActivityAdapter);
            commentTv = (TextView) itemView.findViewById(R.id.tvComment_playActivityAdapter);
            likeNumTv = (TextView) itemView.findViewById(R.id.tvLike_playActivityAdapter);
            userIv = (NetworkImageView) itemView.findViewById(R.id.ivComment_playActivityAdapter);

        }
    }

    // 查看更多评论
    class AdapterFootViewHolder extends RecyclerView.ViewHolder {
        private TextView moreCommentTv;

        public AdapterFootViewHolder(View itemView) {
            super(itemView);
            moreCommentTv = (TextView) itemView.findViewById(R.id.tvMoreComment_playActivityAdapter);
        }
    }
}
